package cn.buyclss.backend.exception;

import cn.buyclss.backend.entity.support.RespResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Cecurio
 * @create: 2019-02-10 21:06
 * 全局异常处理
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 捕捉shiro的异常
    @ExceptionHandler(ShiroException.class)
    public RespResult handle401(ShiroException e) {
        e.printStackTrace();
        return RespResult.failureInstance("权限验证失败，请检查token");
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public RespResult handleUnauthenticatedException(UnauthenticatedException e) {
        e.printStackTrace();
        return RespResult.failureInstance("未登录");
    }

    // 捕捉shiro的异常
    @ExceptionHandler(UnauthorizedException.class)
    public RespResult handleUnauthorizedException(UnauthorizedException e) {
        e.printStackTrace();
        return RespResult.failureInstance("权限验证失败");
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public RespResult exceptionHandler(RuntimeException e) {
        e.printStackTrace();
        return RespResult.failureInstance("系统错误!");
    }

}
